import comSpSkeleton from '../../engines/components/comSpSkeleton';
import gameConfig from '../../engines/configs/gameConfig';
import O1HD, { managers, services } from '../../engines/gameEngine';
import { REWARD_TYPE } from '../../engines/services/userContent/userInterface';
import PageBase from '../Base/PageBase';
import { GAME_EVENT, PAGE } from '../Enum';
import { IInGamePagePrams } from '../Interface';
import mainRoot from '../MainRoot';

const { ccclass, property, menu, disallowMultiple } = cc._decorator;

@ccclass
@disallowMultiple
@menu('游戏页面/游戏中页面')
export default class InGamePage extends PageBase {
    @property({ type: cc.Label, displayName: "提示显示" })
    protected tipsShow: cc.Label = null;
    @property({ type: cc.Label, displayName: "关卡数显示" })
    private levelShow: cc.Label = null;
    @property({ type: cc.Node, displayName: "广告标识" })
    private adIcon: cc.Node = null;
    @property({ type: cc.Node, displayName: "标题外框" })
    private titleKuang: cc.Node = null;
    @property({ type: cc.Node, displayName: "提示按钮" })
    protected tipBtn: cc.Node = null;

    @property({type:cc.Node,tooltip:"提示视频图标"})
    hintVideo:cc.Node = null;

    @property({type:cc.Node,tooltip:"录屏按钮"})
    record:cc.Node = null;

    @property({type:cc.Node,tooltip:"录屏中节点"})
    recordIng:cc.Node = null;

    @property({type:cc.Node,tooltip:"分享录屏按钮"})
    shareRecord:cc.Node = null;

    @property({type:cc.Label,tooltip:"录屏显示文本"})
    lbRecord:cc.Label = null;

    recordFlag:boolean = false;

    recordIsVaild:boolean = false;

    private tipsContent: string = null;
    async OnPageOpen(prams: IInGamePagePrams) {
        super.OnPageOpen(prams);
        this.OnTipsGot();
        this.tipsShow.string = prams.title;
        if (prams.hideTitle > 0) {
            this.tipsShow.node.parent.active = false;
        }

        this.tipBtn.active = prams.tips!=null;
        this.tipsContent = prams.tips;
        this.levelShow.string = mainRoot.LevelSystem.GetCurrLevel(this.m_mode).toString();
        this.initRecord();
    }

    initRecord(){
        this.shareRecord.active = false;
        if(mainRoot.platName == "novelapp"){
            this.record.active = false;
        }
    }

    startRecord(){
        O1HD.getServices().miniGameSrv.getAppApiInstance().gameRecordStart({duration:300});
        this.recordIngAni();
        this.lbRecord.string = "录屏中";
        this.recordFlag = true;
    }

    recordIngAni(){
        let tween = cc.tween(this.record).delay(1).call(()=>{
            this.recordIng.active = !this.recordIng.active;
        });
        cc.tween(this.record).repeatForever(tween).start();
    }

    async stopRecord(){
        cc.Tween.stopAllByTarget(this.record);
        this.recordIng.active = false;
        this.lbRecord.string = "录屏";
        this.recordFlag = false;
        this.recordIsVaild = await O1HD.getServices().miniGameSrv.getAppApiInstance().gameRecordStop();
        if(this.recordIsVaild){
            this.shareRecord.active = true;
        }else{
            O1HD.getModules().uiControlModule.showMessageBox("录屏时间小于三秒");
        }
    }

    onBtnRecord(){
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");
        if(this.recordFlag){
            this.stopRecord();
        }else{
            this.startRecord();
        }
    }

    onBtnShareRecord(){
        if(this.recordIsVaild){
            services().miniGameSrv.getAppApiInstance().shareGameRecordVideo(() => {});
        }else{
            O1HD.getModules().uiControlModule.showMessageBox("录屏时间小于三秒");
        }
    }

    protected onLoad(): void {
        O1HD.on(GAME_EVENT.ON_TIPS_GOT, this.onGetTip.bind(this));
        O1HD.on(GAME_EVENT.ON_ANSWER_GOT, this.onGetAnswer.bind(this));
    }

    protected start(): void {
        this.titleKuangOpen();

        this.titleKuang.y = (services().ossSrv.getPlatform() == "h5" )? -100 : -180
    }

    onGetTip() {
        mainRoot.GotTips = true;
    }

    onGetAnswer() {
        mainRoot.GotAnswer = true;
    }

    titleKuangOpen() {
        let titleWidth: number = this.titleKuang.width;// this.tipsShow.string.length * this.tipsShow.fontSize;
        cc.tween(this.titleKuang).to(0.5, { width: titleWidth + 30 }).start();
        // cc.log(titleWidth);
    }

    OnRestartBtn() {
        O1HD.getManagers().soundMgr.playSound('mainSound/anNiu').then();
        if (O1HD.getServices().userSrv.getPower() - gameConfig.MODE_POWER[0] < 0) {
            mainRoot.LevelSystem.LoadLevel(mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode).bundleName, this.m_mode).then(() => {
                mainRoot.UIModel.showUserPropGet(REWARD_TYPE.POWER, gameConfig.ADD_ONCE_POWER);
            })
            return;
        }
        mainRoot.LevelSystem.LoadLevel(mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode).bundleName, this.m_mode);
        mainRoot.UISystem.CloseAllPage();
        services().userSrv.setPower(-gameConfig.MODE_POWER[this.m_mode], true, true);
    }

    OnOpenPauseBtn() {
        O1HD.getManagers().soundMgr.playSound('mainSound/anNiu').then();
        mainRoot.UISystem.OpenPage(PAGE.PAUSEPAGE);
    }

    OnTipsBtn() {
        if (!PageBase.isPlaySound) {
            PageBase.isPlaySound = true;
            if(mainRoot.GotTips){
                O1HD.emit(GAME_EVENT.ON_BTN_TIPS);
            }else{
                O1HD.getManagers().soundMgr.playSound('mainSound/anNiu').then();
                if(O1HD.getServices().userSrv.getCoins() > 0){
                    O1HD.emit(GAME_EVENT.ON_BTN_TIPS);
                    mainRoot.GotTips = true;
                    this.hintVideo.active = false;
                    O1HD.getServices().userSrv.setCoins(-1,true);
                }else{
                    O1HD.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(()=>{
                        O1HD.emit(GAME_EVENT.ON_BTN_TIPS);
                        this.hintVideo.active = false;
                        mainRoot.GotTips = true;
                    },()=>{
    
                    },null,"看广告获得提示");
                }
            }
            
            // if(mainRoot.GotTips){
            //     O1HD.emit(GAME_EVENT.ON_BTN_TIPS);
            //     // mainRoot.UISystem.OpenPage(PAGE.TIPSPAGE,tipsPrams);
            //     return;
            // }
            // cc.director.pause();
            // services().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(()=>{
            //     // mainRoot.gotTips = true;
            //     // mainRoot.UISystem.OpenPage(PAGE.TIPSPAGE,tipsPrams);
            //     cc.director.resume();
            //     O1HD.emit(GAME_EVENT.ON_BTN_TIPS);
            // },()=>{
            //     cc.director.resume();
            // },()=>{
            //     cc.director.resume();
            // },"看视频获取提示");
        }
    }

    OnTipsGot() {
        // this.adIcon.active = !mainRoot.GotTips;
    }

    OnNextLevelBtn() {
        O1HD.getManagers().soundMgr.playSound('mainSound/anNiu').then();
        // if(O1HD.getServices().userSrv.getPower()-gameConfig.MODE_POWER[0]<0){
        //     mainRoot.UIModel.showUserPropGet(REWARD_TYPE.POWER,gameConfig.ADD_ONCE_POWER);
        //     return;
        // }
        let completeFun = async () => {
            console.warn('重置4');
            mainRoot.GotTips = false;
            mainRoot.GotAnswer = false;
            mainRoot.LevelSystem.AddLevel(this.m_mode);
            this.ClosePage();
            await mainRoot.LevelSystem.LoadLevel(mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode).bundleName, this.m_mode);
            // services().userSrv.setPower(-gameConfig.MODE_POWER[this.m_mode],true,true);
            cc.director.resume();
            mainRoot.UISystem.ClosePage(PAGE.WINPAGE);
        };
        cc.director.pause();
        services().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(completeFun, () => { cc.director.resume(); }, () => { cc.director.resume(); }, "看视频跳关");
        return;
    }
}